package net.dgitc.xingyannian.controller;

import net.dgitc.xingyannian.service.StudentService;
import net.dgitc.xingyannian.util.IdCardEncryptor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * StudentController
 *
 * @author XING&WANG
 * @version 2024/08/26 19:51
 **/
@Controller
@RequestMapping("/student")
public class StudentController {
    private final StudentService studentService;
    public StudentController(StudentService studentService) {
        this.studentService = studentService;
    }
    // 访问 http://localhost:8080/student/index
    @RequestMapping("/index")
    public String index() {
        return "student/index";
    }
    // 展示列表
    @RequestMapping("/list")
    public String list(Model model) {
        // 调用业务层方法，获取学生信息
        var students = this.studentService.getAllStudents();
        // 添加到模型中
        model.addAttribute("students", students);
        // 返回页面
        return "student/list";
    }
    // 通过学生姓名和身份证查询学生信息
    @RequestMapping("/info")
    public String get(String sName, String sPid , Model model) {
        // 调用业务层方法，获取学生信息
        var student = this.studentService.getStudentInfo(sName, sPid);
        if (student == null) {
            // 添加到模型中
            model.addAttribute("msg", "没有找到学生信息!");
            return "student/error";
        }
        //对学生信息中的身份证号进行加密，替换掉出生日期为******
        student.setSPid(IdCardEncryptor.encryptIdCard(student.getSPid()));
        // 添加到模型中
        model.addAttribute("student", student);
        return "student/info";
    }
    // 导入excel方式，批量增加学生信息，返回界面
    @RequestMapping("/import")
    public String importExcel() {
        return "student/import";
    }
    // 接收处理excel，批量增加学生信息，返回列表


}
